package com.mine.modules.vm.controller;

import com.alibaba.excel.EasyExcel;
import com.mine.common.config.exception.MineBootException;
import com.mine.common.domain.vo.Result;
import com.mine.modules.vm.common.constant.BizConstant;
import com.mine.modules.vm.domain.dto.DataHandlingDto;
import com.mine.modules.vm.entity.VmAssets;
import com.mine.modules.vm.entity.VmLayerElement;
import com.mine.modules.vm.entity.VmSensor;
import com.mine.modules.vm.entity.VmSensorElement;
import com.mine.modules.vm.service.IVmAssetsService;
import com.mine.modules.vm.service.IVmLayerElementService;
import com.mine.modules.vm.service.IVmSensorElementService;
import com.mine.modules.vm.service.IVmSensorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/vm/dada/handing")
@RequiredArgsConstructor
@Api(tags = "数据处理")
public class DataHandlingController {

    private final IVmAssetsService vmAssetsService;

    private final IVmSensorService vmSensorService;

    private final IVmSensorElementService vmSensorElementService;

    private final IVmLayerElementService vmLayerElementService;

    @ApiOperation("文件导入")
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/convertToExcelData", method = RequestMethod.POST)
    public Result<?> convertToExcelData(@RequestPart("file") MultipartFile file) throws IOException {
        try {
            if (file == null) {
                return Result.error("参数必填！");
            }
            List<DataHandlingDto> list = EasyExcel.read(file.getInputStream())
                    .head(DataHandlingDto.class)
                    .sheet()
                    .doReadSync();

            String layerId = "1853697632862547970";
            for (DataHandlingDto row : list) {
                if (StringUtils.isNotBlank(row.getElementId()) && StringUtils.isNotBlank(row.getTableName())) {
                    VmAssets vmAssets = vmAssetsService.lambdaQuery().eq(VmAssets::getAssetNumber, row.getElementId()).one();
                    VmSensor vmSensor = vmSensorService.lambdaQuery().eq(VmSensor::getCode, row.getTableName()).one();
                    if (vmAssets != null && vmSensor != null) {
                        vmLayerElementService.save(new VmLayerElement(
                                layerId,
                                vmAssets.getId(),
                                vmAssets.getAssetNumber(),
                                vmAssets.getAssetName(),
                                null,
                                BizConstant.SCENE_ELEMENT_TYPE_ASSET));
                        VmSensorElement vmSensorElement = new VmSensorElement(vmAssets.getId(), vmSensor.getId());
                        vmSensorElementService.save(vmSensorElement);
                    }
                } else {
                    VmAssets baseVmAssets = vmAssetsService.lambdaQuery()
                            .eq(VmAssets::getAssetName, row.getRegion())
                            .eq(VmAssets::getAssetClassify, BizConstant.ASSET_CLASSIFY_OTHER)
                            .one();
                    if (baseVmAssets == null) {
                        baseVmAssets = new VmAssets();
                        baseVmAssets.setAssetName(row.getRegion());
                        baseVmAssets.setAssetClassify(BizConstant.ASSET_CLASSIFY_OTHER);
                        if (vmAssetsService.save(baseVmAssets)) {
                            vmLayerElementService.save(new VmLayerElement(
                                    layerId,
                                    baseVmAssets.getId(),
                                    baseVmAssets.getAssetNumber(),
                                    baseVmAssets.getAssetName(),
                                    null,
                                    BizConstant.SCENE_ELEMENT_TYPE_ASSET));
                        }
                    }
                    VmSensor vmSensor = vmSensorService.lambdaQuery().eq(VmSensor::getCode, row.getTableName()).one();
                    if (vmSensor != null) {
                        VmSensorElement vmSensorElement = new VmSensorElement(baseVmAssets.getId(), vmSensor.getId());
                        vmSensorElementService.save(vmSensorElement);
                    }
                }
            }
        } catch (Exception e) {
            throw new MineBootException("操作失败！");
        }
        return Result.ok("操作成功！");
    }
}